package com.wgchao.identityrightsmanagement.query;

import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectDisabledException;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectNotExistException;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.entity.*;
import com.wgchao.identityrightsmanagement.infrastructure.jpa.repository.*;
import com.wgchao.identityrightsmanagement.query.vo.AccountPermissionVo;
import com.wgchao.identityrightsmanagement.query.vo.PermissionDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @ClassName: PermissionQuery
 * @author: wgchao
 * @createTime: 2020/6/29 3:13 PM
 * @Description: 权限查询
 */
@Service
public class PermissionQuery {
    @Autowired
    private SysResourceProjectRepository sysResourceProjectRepository;
    @Autowired
    private SysPermissionOfficeRepository permissionOfficeRepository;
    @Autowired
    private SysPermissionOfficeFunctionRepository permissionOfficeFunctionRepository;
    @Autowired
    private SysBaseAccountOfficeRepository accountOfficeRepository;
    @Autowired
    private SysBaseDepartmentOfficeRepository departmentOfficeRepository;
    @Autowired
    private SysBaseAccountRepository accountRepository;
    @Autowired
    private SysBaseDepartmentRepository departmentRepository;

    /**
     * 查询部门角色在项目下的授权
     * @param officeCode
     */
    public PermissionDetailVo searchAllPermissionsOfOffice(String officeCode, String departmentCode, String proMark){
        SysResourceProject project = sysResourceProjectRepository.findFirstByProMark(proMark);
        if(project == null){
            throw new ObjectNotExistException("项目不存在");
        }
        List<SysPermissionOffice> permissionOffices = permissionOfficeRepository
                .findAllByOfficeCodeAndDepartmentCodeAndProMarkOrderByMenuMark(officeCode, departmentCode, proMark);
        if(permissionOffices!=null && !permissionOffices.isEmpty()){
            PermissionDetailVo permissionInfo = new PermissionDetailVo();
            permissionInfo.setDepartmentCode(departmentCode);
            permissionInfo.setOfficeCode(officeCode);
            List<String> permissions = new ArrayList<>();
            for(SysPermissionOffice permissionOffice : permissionOffices){
                String pinfo = proMark+"-"+permissionOffice.getMenuMark();
                permissions.add(pinfo);
                //查询功能权限
                List<SysPermissionOfficeFunction> permissionOfficeFunctions = permissionOfficeFunctionRepository
                        .findAllByPid(permissionOffice.getId());
                if(permissionOfficeFunctions!=null && !permissionOfficeFunctions.isEmpty()){
                    for(SysPermissionOfficeFunction permissionOfficeFunction : permissionOfficeFunctions){
                        String pinfo_func = proMark+"-"+permissionOffice.getMenuMark()+"-"+permissionOfficeFunction.getFuncMark();
                        permissions.add(pinfo_func);
                    }
                }
            }
            permissionInfo.setPermissions(new HashSet<>(permissions));
            return permissionInfo;
        }
        return null;
    }

    /**
     * 查询账户在项目下的授权
     * @param username
     */
    public AccountPermissionVo searchAllPermissionsOfAccount(String username, String proMark){
        SysResourceProject project = sysResourceProjectRepository.findFirstByProMark(proMark);
        if(project == null){
            throw new ObjectNotExistException("项目不存在");
        }
        SysBaseAccount account = accountRepository.findFirstByUsernameAndDelFalse(username);
        if(account == null){
            throw new ObjectNotExistException("账户不存在");
        }
        if(account.isDisabled()){
            throw new ObjectDisabledException("账户不可用");
        }

        //查询账户职位
        List<SysBaseAccountOffice> accountOffices = accountOfficeRepository.findAllByUsername(username);
        if(accountOffices==null || accountOffices.isEmpty()){
            return null;
        }
        AccountPermissionVo permissionInfo = new AccountPermissionVo();
        permissionInfo.setDepartmentCode(account.getDepartmentCode());
        permissionInfo.setUsername(username);
        for(SysBaseAccountOffice accountOffice : accountOffices){
            //排除无效权限
            SysBaseDepartmentOffice departmentOffice = departmentOfficeRepository
                    .findFirstByDepartmentCodeAndOfficeCode(account.getDepartmentCode(), accountOffice.getOfficeCode());
            if(departmentOffice == null){
                continue;
            }
            SysBaseDepartment department = departmentRepository.findFirstByCodeAndDelIsFalse(account.getDepartmentCode());
            if(department==null || department.isDisabled()){
                continue;
            }

            //查询权限信息
            List<SysPermissionOffice> permissionOffices = permissionOfficeRepository
                    .findAllByOfficeCodeAndDepartmentCodeAndProMarkOrderByMenuMark(accountOffice.getOfficeCode(), account.getDepartmentCode(), proMark);
            if(permissionOffices!=null && !permissionOffices.isEmpty()){

                List<String> permissions = new ArrayList<>();
                for(SysPermissionOffice permissionOffice : permissionOffices){
                    String pinfo = proMark+"-"+permissionOffice.getMenuMark();
                    permissions.add(pinfo);
                    //查询功能权限
                    List<SysPermissionOfficeFunction> permissionOfficeFunctions = permissionOfficeFunctionRepository
                            .findAllByPid(permissionOffice.getId());
                    if(permissionOfficeFunctions!=null && !permissionOfficeFunctions.isEmpty()){
                        for(SysPermissionOfficeFunction permissionOfficeFunction : permissionOfficeFunctions){
                            String pinfo_func = proMark+"-"+permissionOffice.getMenuMark()+"-"+permissionOfficeFunction.getFuncMark();
                            permissions.add(pinfo_func);
                        }
                    }
                }
                permissionInfo.setPermissions(new HashSet<>(permissions));
            }
        }

        return permissionInfo;
    }

    /**
     * 查询项目下的所有权限
     * @return
     */
    public AccountPermissionVo searchAllPermissionsOfAdmin(String proMark){
        return null;
    }
}
